package importxml;

import java.io.File;
import java.util.Vector;

import org.w3c.dom.CharacterData;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Project {
	
	private String name;
	private String course;
	private String description;
	private boolean done;
	private String grades;
	private String type = "task";
	private String examRoom;
	private String examHours;
	private String examType;
	private boolean examOral;
	private String projectPlanning;
	private Vector<String> projectMembers;
	private Vector<String> deadlines;
	private Vector<String> deadlineDescs;
	private Vector<String> files;
	private boolean valid = true;
	private String dir;
	
	public Project(Element project, String dir) {
		this.dir = dir;
		NodeList nodes = project.getElementsByTagName("name");
		Element element = (Element) nodes.item(0);
		if (element != null) {
			name = getContent(element);
		}
		else {
			valid = false;
		}
		nodes = project.getElementsByTagName("description");
		element = (Element) nodes.item(0);
		if (element != null) {
			description = getContent(element);
		}
		nodes = project.getElementsByTagName("course");
		element = (Element) nodes.item(0);
		if (element != null) {
			course = getContent(element);
		}
		nodes = project.getElementsByTagName("members");
		element = (Element) nodes.item(0);
		if (element != null) {
			NodeList members = element.getElementsByTagName("member");
			handleMembers(members);
		}
		nodes = project.getElementsByTagName("deadlines");
		element = (Element) nodes.item(0);
		if (element != null) {
			NodeList deadlines = element.getElementsByTagName("deadline");
			handleDeadlines(deadlines);
		}
		nodes = project.getElementsByTagName("files");
		element = (Element) nodes.item(0);
		if (element != null) {
			NodeList files = element.getElementsByTagName("file");
			handleFiles(files);
		}
	}
	
	private void handleMembers(NodeList nodes) {
		projectMembers = new Vector<String>();
		type = "project";
		for (int i = 0; i < nodes.getLength(); i++) {
			Element member = (Element) nodes.item(i);
			NodeList contact = member.getElementsByTagName("contact");
			member = (Element) contact.item(0);
			if (member != null) {
				projectMembers.add(getContent(member));
			}
			else {
				valid = false;
			}
		}
	}
	
	private void handleDeadlines(NodeList nodes) {
		deadlines = new Vector<String>();
		deadlineDescs = new Vector<String>();
		for (int i = 0; i < nodes.getLength(); i++) {
			Element deadline = (Element) nodes.item(i);
			NodeList time = deadline.getElementsByTagName("dateTime");
			Element date = (Element) time.item(0);
			if (date != null) {
				deadlines.add(getContent(date));
			}
			else {
				valid = false;
			}
			NodeList desc = deadline.getElementsByTagName("description");
			deadline = (Element) desc.item(0);
			if (deadline != null) {
				deadlineDescs.add(getContent(deadline));
			}
			else {
				valid = false;
			}
		}
	}
	
	private void handleFiles(NodeList nodes) {
		files = new Vector<String>();
		for (int i = 0; i < nodes.getLength(); i++) {
			Element file = (Element) nodes.item(i);
			NodeList path = file.getElementsByTagName("path");
			file = (Element) path.item(0);
			if (file != null) {
				File test = new File(dir + "\\" + getContent(file));
				if (!test.exists()) {
					valid = false;
				}
				files.add(getContent(file));
			}
			else {
				valid = false;
			}
		}
	}
	
	private String getContent(Element e) {
		Node child = e.getFirstChild();
		if (child instanceof CharacterData) {
			CharacterData cd = (CharacterData) child;
			return cd.getData();
		}
		return "?";
	}
	
	public boolean isValid() {
		return valid;
	}
	
	public String getName() {
		return name;
	}
	
	public String getCourse() {
		return course;
	}
	
	public String getDescription() {
		return description;
	}
	
	public boolean getDone() {
		return done;
	}
	
	public void setDone(boolean bool) {
		done = bool;
	}
	
	public String getGrades() {
		return grades;
	}
	
	public void setGrades(String grades) {
		this.grades = grades;
	}
	
	public String getType() {
		return type;
	}
	
	public void setType(String type) {
		this.type = type;
	}
	
	public String getExamRoom() {
		return examRoom;
	}
	
	public void setExamRoom(String room) {
		examRoom = room;
	}
	
	public String getExamHours() {
		return examHours;
	}
	
	public void setExamHours(String hours) {
		examHours = hours;
	}
	
	public String getExamType() {
		return examType;
	}
	
	public void setExamType(String type) {
		examType = type;
	}
	
	public boolean getExamOral() {
		return examOral;
	}
	
	public void setExamOral(boolean oral) {
		examOral = oral;
	}
	
	public String getProjectPlanning() {
		return projectPlanning;
	}
	
	public void setprojectPlanning(String planning) {
		projectPlanning = planning;
	}
	
	public int getNumberOfMembers() {
		return projectMembers.size();
	}
	
	public String getProjectMember(int index) {
		return projectMembers.get(index);
	}
	
	public int getNumberOfDeadlines() {
		return deadlines.size();
	}
	
	public String getDeadline(int index) {
		return deadlines.get(index);
	}
	
	public String getDeadlineDesc(int index) {
		return deadlineDescs.get(index);
	}
	
	public int getNumberOfFiles() {
		return files.size();
	}
	
	public String getFile(int index) {
		return files.get(index);
	}
	
}
